﻿using Kinlo.Model;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Comm.Helper
{
    public class ExcelHelper
    {
        public static bool SaveToExcel(string filePath, ObservableCollection<AfterWeightFirst> rows)
        {
            if (!string.IsNullOrEmpty(filePath) && rows.Count > 0)
            {
                NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
                var ObjType = rows[0].GetType();
                var properties = ObjType.GetProperties();
                List<PropertyInfoHelper> PropertyInfos = new List<PropertyInfoHelper>();
                foreach (var property in properties)
                {
                   var customAttrs = property.GetCustomAttributes(typeof(PrintExportAttribute),false);
                   // var customAttrs = property.GetCustomAttributes<PrintExportAttribute>();
                    foreach (var attr in customAttrs)
                    {
                        var att = attr as PrintExportAttribute; 
                        if (att.Visibility == true)
                        {
                            PropertyInfos.Add(new PropertyInfoHelper { PropertyInfo = property, Description = att.Description, Order = att.Order });
                        }
                    }
                }
                if (PropertyInfos.Count > 0)
                {
                    PropertyInfos = PropertyInfos.OrderBy(s => s.Order).ToList();

                    //创建SHEET头
                    var headerRow = sheet.CreateRow(0);
                    for (var i = 0; i < PropertyInfos.Count; i++)
                    {
                        headerRow.CreateCell(i).SetCellValue(PropertyInfos[i].Description);
                    }

                    var innerCellStye1 = book.CreateCellStyle();
                    innerCellStye1.FillPattern = FillPattern.SolidForeground;
                    innerCellStye1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;

                    var innerCellStye2 = book.CreateCellStyle();
                    innerCellStye2.FillPattern = FillPattern.SolidForeground;
                    innerCellStye2.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Green.Index;
                    //创建SHEET体
                    for (var j = 0; j < rows.Count; j++)
                    {
                        var bodyRow = sheet.CreateRow(j + 1);
                        for (var i = 0; i < PropertyInfos.Count; i++)
                        {
                            var r = rows[j];
                            string val;
                            try
                            {
                                var value = PropertyInfos[i].PropertyInfo.GetValue(r);
                                if (value is DateTime)
                                {
                                    var date = (DateTime)value;
                                    val = date.ToString("yyyy-MM-dd HH:mm:ss");
                                }
                                else
                                {
                                    val = value.ToString();
                                }
                            }
                            catch
                            {
                                val = "";
                            }
                            var cell = bodyRow.CreateCell(i);
                            if (PropertyInfos[i].Description == "结果")
                            {
                                bool isGoods;
                                if (bool.TryParse(val, out isGoods))
                                {
                                    if (isGoods)
                                    {
                                        val = "OK";
                                        cell.CellStyle = innerCellStye2;

                                    }
                                    else
                                    {
                                        val = "NG";
                                        cell.CellStyle = innerCellStye1;
                                    }
                                }
                                else
                                {
                                    val = "NG";
                                    cell.CellStyle = innerCellStye1;
                                }

                            }
                            cell.SetCellValue(val);
                        }
                    }
                    using (var ms = new MemoryStream())
                    {
                        book.Write(ms);
                        using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            var data = ms.ToArray();
                            fs.Write(data, 0, data.Length);
                            fs.Flush();
                        }
                        book = null;
                    }
                    return true;
                }
                else
                {
                    throw new Exception("找不到相关的导出到Excel的属性");
                }
            }
            return false;
        }

        public static bool SaveToFrontAfterWeight(string filePath, List<AfterWeightFirst> rows, string type)
        {
             
            return false;
        }
    }

    public class PropertyInfoHelper
    {
        public PropertyInfo PropertyInfo { get; set; }
        public int Order { get; set; }
        public String Description { get; set; }
    }
}
